<?php
class sis_sijaka_historis_bunga_model extends MY_Model {
	var $model = 'sis_sijaka_historis_bunga';
	
	function GetJSON($as_id_sijaka = '', $as_no_sertifikat='', $an_periode_ro=0)
	{
		$ls_sql	= "	SELECT	A.ID_SIJAKA, A.NO_SERTIFIKAT, A.PERIODE_RO, A.PERIODE_BUNGA, 
							TO_CHAR(A.TGL_AWAL,'DD/MM/YYYY') TGL_AWAL, TO_CHAR(A.TGL_AKHIR,'DD/MM/YYYY') TGL_AKHIR, A.JML_HARI, 
							A.JML_DANA, A.PERSEN_PPH, A.JML_PPH, A.JML_BUNGA, A.ST_BUNGA, TO_CHAR(A.TGL_PROSES,'DD/MM/YYYY') TGL_PROSES
					FROM	SIS_SIJAKA_HISTORIS_BUNGA A 
							LEFT JOIN SIS_SIJAKA_MASTER B ON A.ID_SIJAKA = B.ID_SIJAKA 
					WHERE	1 = 1 
							".($as_id_sijaka == '' ? '' : " AND A.ID_SIJAKA = '".$as_id_sijaka."' ")." 
							".($as_no_sertifikat == '' ? '' : " AND A.NO_SERTIFIKAT = '".$as_no_sertifikat."' ")." 
							".((int)$an_periode_ro == 0 ? '' : " AND A.PERIODE_RO = ".$an_periode_ro." ")." 
					ORDER BY ".(post('sort')=='' ? " A.NO_SERTIFIKAT ASC, A.PERIODE_RO, A.PERIODE_BUNGA " : post('sort'))." ".(post('order')=='' ? 'ASC' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetDetail($as_id_sijaka = '', $as_no_sertifikat='', $an_periode_ro=0)
	{
		
		$ls_sql	= "	SELECT	A.ID_SIJAKA, A.NO_SERTIFIKAT, A.PERIODE_RO, A.PERIODE_BUNGA, 
							TO_CHAR(A.TGL_AWAL,'DD/MM/YYYY') TGL_AWAL, TO_CHAR(A.TGL_AKHIR,'DD/MM/YYYY') TGL_AKHIR, A.JML_HARI, 
							A.JML_DANA, A.PERSEN_PPH, A.JML_PPH, A.JML_BUNGA, A.ST_BUNGA, TO_CHAR(A.TGL_PROSES,'DD/MM/YYYY') TGL_PROSES
					FROM	SIS_SIJAKA_HISTORIS_BUNGA A 
							LEFT JOIN SIS_SIJAKA_MASTER B ON A.ID_SIJAKA = B.ID_SIJAKA 
					WHERE	1 = 1 
							".($as_id_sijaka == '' ? '' : " AND A.ID_SIJAKA = '".$as_id_sijaka."' ")." 
							".($as_no_sertifikat == '' ? '' : " AND A.NO_SERTIFIKAT = '".$as_no_sertifikat."' ")." 
							".((int)$an_periode_ro == 0 ? '' : " AND A.PERIODE_RO = ".$an_periode_ro." ")." 
					ORDER BY A.NO_SERTIFIKAT ASC, A.PERIODE_RO, A.PERIODE_BUNGA ASC";

		$result = $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return NULL;
		}
	}
	
	function Add($arr_data){
		$this->db->insert('sis_sijaka_historis_bunga', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('sis_sijaka_historis_bunga', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_id_sijaka, $as_no_sertifikat, $an_periode_ro, $an_periode_bunga){
		$result = $this->db->get_where('sis_sijaka_historis_bunga', array('id_sijaka' => $as_id_sijaka,  'no_sertifikat' => $as_no_sertifikat, 'periode_ro' => (int)$an_periode_ro, 'periode_bunga' => (int)$an_periode_bunga));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_id_sijaka, $as_no_sertifikat, $an_periode_ro, $an_periode_bunga)
	{
		$result = $this->db->query("SELECT	A.ID_SIJAKA, A.NO_SERTIFIKAT, A.PERIODE_RO, A.PERIODE_BUNGA, 
											TO_CHAR(A.TGL_AWAL,'DD/MM/YYYY') TGL_AWAL, TO_CHAR(A.TGL_AKHIR,'DD/MM/YYYY') TGL_AKHIR, A.JML_HARI, 
											A.JML_DANA, A.PERSEN_PPH, A.JML_PPH, A.JML_BUNGA, A.ST_BUNGA, TO_CHAR(A.TGL_PROSES,'DD/MM/YYYY') TGL_PROSES
									FROM	SIS_SIJAKA_HISTORIS_BUNGA A 
											LEFT JOIN SIS_SIJAKA_MASTER B ON A.ID_SIJAKA = B.ID_SIJAKA 
									WHERE	A.ID_SIJAKA = '".$as_id_sijaka."' AND A.NO_SERTIFIKAT = '".$as_no_sertifikat."' AND A.PERIODE_RO = ".$an_periode_ro." AND A.PERIODE_BUNGA = ".$an_periode_bunga);
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM sis_sijaka_historis_bunga WHERE ID_SIJAKA = '".$arr_rule['id_sijaka']."' AND NO_SERTIFIKAT = '".$arr_rule['no_sertifikat']."' AND PERIODE_RO = ".$arr_rule['periode_ro']." AND PERIODE_BUNGA = ".$arr_rule['periode_bunga']." AND NVL(ST_BUNGA,'0') <> '0'"))>0)
			{
				return $this->error_delete.' [id: '.$arr_rule['id_sijaka'].", ".$arr_rule['no_sertifikat'].", ".$arr_rule['periode_ro'].", ".$arr_rule['periode_bunga']." ]<br />";
			}else{
				$this->db->delete('sis_sijaka_historis_bunga', array('id_sijaka' => $arr_rule['id_sijaka'],  'no_sertifikat' => $arr_rule['no_sertifikat'], 'periode_ro' => (int)$arr_rule['periode_ro'], 'periode_bunga' => (int)$arr_rule['periode_bunga']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>